package com.chatdating.controller;

import com.chatdating.entity.pojo.Result;
import com.chatdating.entity.vo.UserByNameVo;
import com.chatdating.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/person")
public class PersonalPageController {
    @Autowired
    private UserService userService;
    /*
    * 根据用户名查询用户信息
     */
    @GetMapping("/getinfo")
    public ResponseEntity<Result<UserByNameVo>> getUserInfo
    (@RequestParam String  username
     ) {
        try {
            UserByNameVo userByNameVo = userService.getUserInfo(username);
            if(userByNameVo == null){
                return ResponseEntity.ok(Result.success(null));
            }
            return ResponseEntity.ok(Result.success(userByNameVo));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }

    }
    /*
    * 修改用户信息
     */
    @PostMapping("/updateinfo")
    public ResponseEntity<Result<String>> updateUserInfo(@RequestBody UserByNameVo user){
        try {
            userService.updateUser(user);
            return ResponseEntity.ok(Result.success("修改成功"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }

    }
    @GetMapping("/delMyUser")
    public ResponseEntity<Result<String>> delMyUser(@RequestParam String username){
        try {
            userService.deleteUser(username);
            return ResponseEntity.ok(Result.success("删除成功"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }

    }
}
